<?php
session_start();
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>汽车之家论坛</title>
    <link href="css/slide.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="singup/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/easydialog.css"/>
    <link rel="stylesheet" type="text/css" href="css/easyui.css">
    <link rel="stylesheet" type="text/css" href="css/icon.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <style>
        .subTitle { width: 70%; float: left; color: black; font-size: 18px; font-weight: bold; margin: 8px 0 8px 20px; }

        .hits { width: 25%; float: right; color: gray; line-height: 35px;}

        .subContents { border-top: 1px solid #cccccc; padding: 8px; }

        .subInfo { width: 20%; min-width: 130px; float: left; text-align: center; }

        .topicContents { width: 79%; float: left; border-left: solid 1px #cccccc; min-height:260px;}

        .subInfoPic { width: 80%; min-width: 120px; }

        .subInfo ul li:first-child { color: #1c8ee5; font-weight: bold; font-size: 14px; }

        .subInfo ul li { line-height: 25px; }

        .topicContentsTitle { border-bottom: 1px solid #ccc; margin-bottom: 18px; font-size: 16px; font-weight: bold; padding: 8px; }

        .topicContentsC { padding: 8px; line-height: 25px;min-height: 150px; }
        .sign{margin: 10px;border-top:1px dashed #cccccc; padding-top: 5px;}
    </style>
</head>
<body>
<script type="text/javascript" charset="utf-8" src="u/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="u/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="u/lang/zh-cn/zh-cn.js"></script>
<div class="top">
    <?php include_once("TOP.php") ?>
    <?php include_once('INFO.php'); ?>
    <?php
    $id = $_GET['id'];
    include_once("conn.php");
    //获取当前贴子的点击次数和回复次数
    $sub = new Bbs();
    $sub->update('mainTopic',1,"id=$id");  //如果action=1表明，是更新点击次数
    $title = $sub -> anyQuery("mainTopic","id",$id);
    $replyNum = $sub -> query("select 1 from subtopic where maintopicid = $id",0);
    ?>
    <div class="mainContent">
        <div class="subTitle"><?php echo $title['info'][0]['title'] ?></div>
        <div class="hits">点击次数：<?php echo $title['info'][0]['hits'] ?>
            &nbsp;&nbsp;&nbsp;&nbsp;回复次数：<?php echo $replyNum['total']; ?></div>
        <div class="clear"></div>
        <div class="subContents">
            <div class="subInfo">
                <ul>
                    <?php
                    $info1 = $sub->getInfo($title['info'][0]['userid'],0);
                    ?>
                    <li><?php echo $info1['info']['nickname'] ?></li>
                    <li><img class="subInfoPic" src="tx/<?php echo $info1['info']['pic'] ?>"</li>
                    <li>注册日期：<?php echo date("Y-m-d", strtotime($info1['info']['postDate'])) ?></li>
                    <li>等级：<?php echo $info1['info']['grade'] ?></li>
                    <li></li>
                </ul>
            </div>
            <div class="topicContents">
                <div class="topicContentsTitle"> <?php echo $title['info'][0]['title'] ?></div>
                <div class="topicContentsC"><?php echo $title['info'][0]['contents'] ?></div>
                <div class="sign">用户IP:<?php echo $title['info'][0]['ip'] ?>&nbsp;&nbsp;
                    <?php if(isset($_SESSION['admin']) and $_SESSION['admin']==1){echo "<a href='delTopic.php?id=".$title['info'][0]['id']."&action=1'>删除本贴</a>";}?></div>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
    <?php
    $sql = "select * from subtopic where maintopicid = $id";
    $result = $sub->query($sql); //省略第二个参数，则第二个参数取默认值1，表示要取内容
    foreach ($result['result'] as $info){
    ?>
        <div class="subContents">
            <div class="subInfo">
                <ul>
                    <?php
                    $sql = "select * from userinfo where id = " . $info['userid'];
                    $result1 = $sub->query($sql);
                    $info1 = $result1['result'][0];
                    ?>
                    <li><?php echo $info1['nickname'] ?></li>
                    <li><img class="subInfoPic" src="tx/<?php echo $info1['pic'] ?>"</li>
                    <li>注册日期：<?php echo date("Y-m-d", strtotime($info1['postDate'])) ?></li>
                    <li>等级：<?php echo $info1['grade'] ?></li>
                    <li></li>
                </ul>
            </div>
            <div class="topicContents">
                <div class="topicContentsC">
                    <?php
                    if($info['quoteid']){  //说明此条回复是引用回复，则显示被引用的内容
                        $sql = "select contents from subtopic where id = ".$info['quoteid'];
                        $quotReply = $sub->query($sql,1);
                    ?>
                    <fieldset>
                        <legend>引用回复</legend>
                        <?php echo $quotReply['result'][0]['contents']?>
                    </fieldset>
                    <?php }
                    ?>
                    <?php echo $info['contents'] ?>
                </div>
                <div class="sign">
                    <b>用户IP：</b><?php echo $info['ip'] ?>&nbsp;&nbsp;
                    <?php if(isset($_SESSION['admin']) and $_SESSION['admin']==1){
                        echo "<a href='delTopic.php?id=".$info['id']."&action=2'>删除本贴</a>";
                    }?>&nbsp;&nbsp;&nbsp;
                    <?php if(isset($_SESSION['loginUserID']) and $_SESSION['loginUserID']){
                        echo "<span onclick='quoteReply(".$info['id'].")'><a href='#editor'>回复TA</a></span>";
                    }?>
                </div>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
    <?php }?>
        <script id="editor" type="text/plain" style="width:100%;height:200px;" ></script>
    </div>
    <div style="width: 80%;margin: 10px auto">
        <div class="count">
        <span class="newPost" onclick="newReply(1);">引用回贴</span>
        <span class="newPost" onclick="newReply(0);" style="margin-right:10px;">直接回贴</span>
        <span class="newPost" onclick="location.href='index.php'">返回首页</span>
        </div>
    </div>
    <div class="bot">版权所有 汽车之家</div>
    <div id="registerForm" class="form_wrapper" style="width: 550px;">
        <form class="register">
            <h3>用户注册</h3>
            <div class="column">
                <div>
                    <label>用户名:</label>
                    <input type="text" name="username" id="username"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>昵称:</label>
                    <input type="text" name="nickname" id="nickname"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>出生日期:</label>
                    <div class="birthday"><input type="text" name="birthday" id="birthday"/>
                        <span class="error">This is an error</span></div>
                </div>
            </div>
            <div class="column">
                <div>
                    <label>选择头像:</label>
                    <input type="file" name="pic" id="pic"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>电子信箱:</label>
                    <input type="text" name="email" id="email"/>
                    <span class="error">This is an error</span>
                </div>
                <div>
                    <label>密码:</label>
                    <input type="password" name="pw" id="pw"/>
                    <span class="error">This is an error</span>
                </div>
            </div>
            <div class="bottom">
                <div class="remember"></div>
                <input type="button" value="注册" onclick="singup();"/>
                <a href="javascript:void(0);" rel="login" class="linkform" id="showLogin">已经有帐号了? 点击登录</a>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </form>
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div id="loginFrom" class="form_wrapper">
        <form class="login">
            <h3>用户登录</h3>
            <div>
                <label>用户名:</label>
                <input type="text" id="username2"/>
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码: <a href="forgot_password.html" rel="forgot_password"
                              class="forgot linkform">忘记密码?</a></label>
                <input type="password" id="pw2"/>
                <span class="error">This is an error</span>
            </div>
            <div class="bottom">
                <div class="remember"><input type="checkbox" id="rememberMe"/><span>保存登录状态</span></div>
                <input type="button" value="登录" onclick="login();">
                <a href="javascript:void(0);" rel="register" class="linkform" id="showRegister">还没有帐号? 点击注册</a>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <div class="clear"></div>
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.form.min.js"></script>
    <script src="js/jquery.easyui.min.js"></script>
    <script src="js/responsiveslides.min.js"></script>
    <script src="js/easydialog.min.js"></script>
    <script src="js/easyui-lang-zh_CN.js"></script>
    <script src="js/site.js"></script>
    <script>
        var hasLogin = 0, hasLogedUsername = '';
        var rememberMe = 0; //默认不保存登录信息
        var quoteID = "null"; //默认表示不引用回复
        <?php
        if ((isset($_SESSION['loginUser']) and $_SESSION['loginUser'] != '')) {  //说明当前处于登录状态
            echo "var hasLogin = 1;";
            echo "var hasLogedUsername = '" . $_SESSION['loginUser'] . "';";
        } else if (isset($_COOKIE['hasLogedUsername']) and $_COOKIE['hasLogedUsername'] != '') { //说明当前已经保存了登录状态
            echo "var hasLogin = 1;";
            echo "var hasLogedUsername = '" . $_COOKIE['hasLogedUsername'] . "';";
        }
        ?>
        function  newReply(action) {
            if(!hasLogin){
                alert('只有登录后才能回复贴子！');
                return;
            }
            if(action == 0){
                quoteID = "null";
            }
            getContent();
        }
        function login() {
            if ($("#username2").val() == '' || $("#pw2").val() == '') {
                alert('用户名和密码必须填写！');
                return;
            }
            if ($("#rememberMe").prop("checked") == true) { //表明选中了保存登录
                rememberMe = 1;
            }
            $.ajax({
                url: 'postLogin.php',
                data: {username: $("#username2").val(), pw: $("#pw2").val(), rememberMe: rememberMe},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.error == 1) {
                        alert('登录失败');
                    }
                    else {
                        hasLogin = 1;
                        $("#username1").text(d.userName);
                        $("#nickname1").text(d.nickName);
                        $("#singUpDate").text(d.singUpDate);
                        $("#total").text(d.total);
                        $("#lastPostTime").text(d.lastPostTime);
                        $("#lastReplyTime").text(d.lastReplyTime);
                        $("#lastTitle").text(d.lastTitle);
                        $("#lastReply").text(d.lastReply);
                        $("#username2").val('');
                        $("#pw2").val('');
                        $(".tx img").attr('src', 'tx/' + d.pic);
                        $(".info").show();
                        easyDialog.close();
                        showLogout();
                    }
                }
            })
        }
        function singup() {
            var errLog = 0;
            var errMsg = '';
            if ($("#username").val() == '') {
                errLog = 1;
                errMsg += "用户名必须填写！\n";
            }
            if ($("#pw").val() == '') {
                errLog = 1;
                errMsg += "密码必须填写！\n";
            }
            if ($("#pic").val() == '') {
                errLog = 1;
                errMsg += "头像必须选择！\n";
            }
            if ($("#birthday").datebox('getValue') == '') {
                errLog = 1;
                errMsg += "生日必须选择！\n";
            }
            if (errLog == 1) {
                alert('发现错误：\n' + errMsg);
            }
            else {
                $("#myUpload").ajaxSubmit({
                    dataType: 'json',
                    success: function (data) {
                        if (data.error == 0)
                            postSingup(data.fileName);
                        else {
                            alert(data.errMsg);
                        }
                    },
                    error: function (xhr) {
                        alert("头像上传失败");
                    }
                });
            }
        }
        function postSingup(filename) {
            $.ajax({
                url: 'postSingup.php',
                data: {
                    username: $("#username").val(),
                    nickname: $("#nickname").val(),
                    email: $("#email").val(),
                    pw: $("#pw").val(),
                    birthday: $("#birthday").datebox('getValue'),
                    pic: filename
                },
                dataType: 'json',
                type: 'post',
                success: function (d) {
                    if (d.error == 1) {
                        alert(d.errMsg)
                    }
                    else {
                        alert('注册成功！');
                        $("#username").val('');
                        $("#pw").val('');
                        $("#email").val('');
                        $("#pic").val('');
                        $("#nickname").val('');
                        $("#birthday").datebox('setValue', '')
                        easyDialog.close();
                    }
                }
            })
        }
        function quoteReply(id){
            quoteID = id;
        }
        $(document).ready(function () {
            //先判断 是否已经登录。如果已经登录，则直接显示info
            if (hasLogin) { //说明已经登录
                $.ajax({
                    url: 'getInfo.php',
                    data: {username: hasLogedUsername},
                    type: "post",
                    dataType: 'json',
                    success: function (d) {
                        $("#username1").text(d.userName);
                        $("#nickname1").text(d.nickName);
                        $("#singUpDate").text(d.singUpDate);
                        $("#total").text(d.total);
                        $("#lastPostTime").text(d.lastPostTime);
                        $("#lastReplyTime").text(d.lastReplyTime);
                        $("#lastTitle").text(d.lastTitle);
                        $("#lastReply").text(d.lastReply);
                        $(".tx img").attr('src', 'tx/' + d.pic);
                        $(".info").show();
                        showLogout();
                    }
                })
            }
            $("#pic").wrap("<form id='myUpload' action='picUpload.php'  method='post' enctype='multipart/form-data'></form>");
            $('#birthday').datebox({
                required: true,
                panelWidth: 200
            });
            $("#slider").responsiveSlides({
                auto: true,
                pager: false,
                nav: true,
                speed: 500,
                namespace: "slide_container"
            });
            $("#login").click(function () {
                easyDialog.open({
                    container: 'loginFrom',
                    fixed: false
                });
            });
            $("#showLogin").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'loginFrom',
                    fixed: false
                });
            })
            $("#showRegister").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'registerForm',
                    fixed: false
                });
            });
        })
    </script>
    <script type="text/javascript">

        //实例化编辑器
        //建议使用工厂方法getEditor创建和引用编辑器实例，如果在某个闭包下引用该编辑器，直接调用UE.getEditor('editor')就能拿到相关的实例
        var ue = UE.getEditor('editor');

        function getContent() {
            if(hasContent()) {
                var lastContents = UE.getEditor('editor').getContent();
                //异步保存发表的贴子
                $.ajax({
                    url:'newReply.php',
                    type:'post',
                    dataType:'json',
                    data:{contents:lastContents,maintopicid:<?php echo $id?>,quoteid:quoteID},
                    success:function (d) {
                        if(d.error == 1){
                            alert(d.errMsg);
                        }
                        else{
                            alert('回贴成功');
                            location.reload();
                        }
                    }
                })
            }
            else{
                alert('你还没有填写内容哦');
                return;
            }
        }
        function hasContent() {
            var result = UE.getEditor('editor').hasContents();
            return result;
        }

    </script>
</body>
</html>